
$(function() {
	var exten = {
			source: function( request, response ) {
				$.ajax({
					url: 'emp.php?do=Exten.ajax',
					dataType: "json",
					data: {
						featureClass: "P",
						style: "full",
						maxRows: 12,
						q: request.term
					},
					success: function( data ) {
						response( $.map( data, function( item ) {
							return {
								label: item.exten + " [" + item.emp_t + ", " + item.dept_t + "]",
								value: item.exten
							}
						}));
					}
				});
			},
			
			minLength: 1
		};
	
	$($("input[id^='src']:visible")).autocomplete(exten);

	$($("input[id^='dst']:visible")).autocomplete(exten);
});

function view(url, title, xy, src, dst, uniqueid) {
	var number = _number(dst);
		
	if (is_blacklist(src, number)) {
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"发邮件": function() { 
				$(this).dialog("close");
				
				var url = 'tel.php?do=Cdr.send&uniqueid=' + uniqueid; 
				// alert(url);
				
				sendmail(url, '发送邮件', '480x280');
			}			
		});
	} else {
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},

			"加入黑名单": function() {
				$(this).dialog("close");

				var url = 'tel.php?do=Black.add&exten=' + src + "&number=" + number; 
				// alert(url);
				
				blacklist(url, '添加黑名单', '480x280');
			},
			
			"发邮件": function() { 
				$(this).dialog("close");
				
				var url = 'tel.php?do=Cdr.send&uniqueid=' + uniqueid; 
				// alert(url);
				
				sendmail(url, '发送邮件', '480x280');
			}
		});
	}
	
	dialog_view(url, title, xy);
}

function _number(s) {
	var regex;

	// 
	regex = /^(9179090)(\d{7,})$/
	if (regex.test(s)) {
		var a = s.substring(7);
		var r = /^1(3|4|5|7|8)(\d{9})$/
		if (r.test(a)) {
			return a;
		} else {
			return '0' + a;
		}
	}

	// 
	regex = /^(1701186)(\d{7,})$/
	if (regex.test(s)) {
		return s.substring(7);
	}

	// 
	regex = /^(9)(\d{7,})$/
	if (regex.test(s)) {
		return s.substring(1);
	}

	return s;
}

function is_blacklist(exten, code) {
	if (!is_exten(exten)) {
		return true;
	}

	if (!is_code22(code)) {
		return true;
	}

	return false;
}

function is_exten(s) {
	var regex;

	// 分机 2029 6595 8595
	regex = /^(2|6|8)(\d{3})$/
	if (regex.test(s)) {
		return true;
	}

	return false;
}

function is_code22(s) {
	var regex;

	// 本地号码 13760120595
	regex = /^1(3|4|5|7|8)(\d{9})$/
	if (regex.test(s)) {
		return true;
	}

	// 本地号码 89809672-6595
	regex = /^(\d{7,})$/
	if (regex.test(s)) {
		return true;
	}

	return false;
}

function blacklist(url, title, xy) {	
	$('#dialog_add').dialog('option', 'buttons', {

		"关闭": function() {
			$(this).dialog("close");
		},

		"添加": function() {
			if (!confirm("确认是否添加？")) {
				return false;
			}
			
			var form = $("#form_add")[0];

			var body = { };
			body.from = form.from.value;
			body.exten = form.exten.value;
			body.number = form.number.value;
			body.zone = form.zone.value;
			body.content = form.content.value;
			body.status = form.status.value;

			$(this).dialog("close");

			$.post('tel.php?do=Black.addPost', body, function(data) {
				alert("添加成功！");
				// $('#result').html(data);
			});
		}
	});

	dialog_add(url, title, xy);
}

function sendmail(url, title, xy) {	
	$('#dialog_add').dialog('option', 'buttons', {

		"关闭": function() {
			$(this).dialog("close");
		},

		"发送": function() {
			if (!confirm("确认是否发送？")) {
				return false;
			}
			
			var form = $("#form_add")[0];

			var body = { };
			body.uniqueid = form.uniqueid.value;
			body.exten = form.exten.value;
			body.number = form.number.value;
			
			body.email = form.email.value;
			body.content = form.content.value;

			$(this).dialog("close");

			$.post('tel.php?do=Cdr.sendPost', body, function(data) {
				alert("发送成功！");
			});
		}
	});

	dialog_add(url, title, xy);
}

function contact(e, json) {
	
	var id = json.id;
	var info = json.info;
	var info_id = json.info_id;
	
	var c1 = 'contact.php?do=Customer.view&id=' + info_id;
	var c2 = 'contact.php?do=Supplier.view&id=' + info_id;

	var c8 = 'info.php?do=Contact.view&id=' + id;
	
	var a1 = ['客户信息', 'url', 'Common.dialog_view_', '650x420'];
	var a2 = ['供应商', 'url', 'Common.dialog_view_', '650x420'];

	var a8 = ['联系信息', 'url', 'Common.dialog_view_', '650x400'];
	
	if (info == 1) {
		var s = c1 + ", " + c8;

		return popup(e, [a1, a8], s);
	}

	if (info == 2) {
		var s = c2 + ", " + c8;

		return popup(e, [a2, a8], s);
	}

	var s = c8;

	return popup(e, [a8], s);
}

var Cdr = {
	ss: 2,
	popup: function (e, p2, id) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);
	
		var c2 = 'tel.php?do=Cdr.view&id=' + id + p2;
		var c3 = 'tel.php?do=Black.add&id=' + id;

		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '550x320'];
		var a3 = ['加入黑名单', 'url', 'Cdr.dialog_black', '550x320'];

		var s = c2 + ", " + c3;

		return popup(e, [a2, a3], s);	
	},
	
	sunxy: function (e, p2, id) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'tel.php?do=Cdr.edit&id=' + id;	
		var c2 = 'tel.php?do=Cdr.view&id=' + id + p2;
		var c3 = 'tel.php?do=Black.add&id=' + id;

		var a1 = ['编辑信息', 'url', 'Cdr.dialog_edit_', '550x320'];
		var a2 = ['查看信息', 'url', 'Cdr.dialog_view_', '550x320'];
		var a3 = ['加入黑名单', 'url', 'Cdr.dialog_black', '550x320'];

		var s = c2 + ", " + c3;

		return popup(e, [a2, a3], s);	
	},
	
	

	dialog_view_: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},

			"加入黑名单": function() {
				$(this).dialog("close");

				var id = $.cookie('id_');
				var url = 'tel.php?do=Black.add&id=' + id;
				
				blacklist(url, '添加黑名单', '500x300');
			},
			
			"发邮件": function() { 
				$(this).dialog("close");
				
				var id = $.cookie('id_');
				
				var url = 'tel.php?do=Cdr.send&uniqueid=' + id;
				
				sendmail(url, '发送邮件', '480x280');
			}
		});
		
		dialog_view(url, title, xy);
	},

	dialog_black: function (url, title, xy) {	
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.from = form.from.value;
				body.exten = form.exten.value;
				body.number = form.number.value;
				body.zone = form.zone.value;
				body.content = form.content.value;
				body.status = form.status.value;

				$(this).dialog("close");

				$.post('tel.php?do=Black.addPost', body, function(data) {
					alert("添加成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'tel.php?do=Cdr.delete' + p2;
		var c2 = 'tel.php?do=Cdr.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	}
};
